﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using EOS.Framework.Web.Mvc.Rendering;

namespace EOS.Framework.Web.Mvc.Controls
{
    public class Hyperlink : MarkupControl
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _href;

        public string Href
        {
            get { return this._href; }
            set { this._href = value; }
        }

        public Hyperlink(string href)
        {
            if (string.IsNullOrEmpty(href))
            {
                throw new ArgumentException(href);
            }

            this._href = href;
        }

        public Hyperlink(string href, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(name);
            }

            if (string.IsNullOrEmpty(href))
            {
                throw new ArgumentException(href);
            }

            this._name = name;
            this._href = href;
        }

        protected override void OnRender(IXhtmlWriter writer, PageContext context)
        {
            if (TagName.ToLower(CultureInfo.CurrentCulture).Equals("a") == false)
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.InvalidTagName, "a"));
            }

            WriteBeginAttachedTag(writer, new string[] { "href" });
            writer.WriteAttribute("href", ToAbsolutePath(_href));

            if (string.IsNullOrEmpty(_name))
            {
                Markup.RenderMarkup(writer, context);
            }
            else
            {
                writer.WriteString(_name);
            }

            WriteEndAttachedTag(writer);
        }
    }
}
